package com.maoye.ossp.biz.statement.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hotent.base.api.query.QueryFilter;
import com.maoye.ossp.biz.statement.entity.StatementOrder;
import com.maoye.ossp.biz.statement.entity.StatementSelfrunNote;
import com.maoye.ossp.biz.statement.entity.StatementReadJustNote;
import com.maoye.ossp.biz.statement.entity.StatementReceiveNote;
import com.maoye.ossp.biz.statement.entity.StatementSelfrun;
import com.maoye.ossp.biz.statement.service.StatementOrderService;
import com.maoye.ossp.biz.statement.service.StatementReadJustNoteService;
import com.maoye.ossp.biz.statement.service.StatementReceiveNoteService;
import com.maoye.ossp.biz.statement.service.StatementSelfrunService;
import com.maoye.ossp.biz.statement.util.BizStatementConstant;
import com.maoye.ossp.common.entity.Page;
import com.maoye.ossp.common.web.BaseController;
/**
 * 自营对账单controller类
 * @author liangtao  2014-06-09
 *
 */
@Controller
@RequestMapping(value = "biz/statement/statementSelfrun/")
public class StatementSelfrunController extends BaseController {
	
	@Resource
	private StatementSelfrunService statementSelfrunService;
	@Resource
	private StatementReceiveNoteService  statementReceiveNoteService;
	@Resource
	private StatementReadJustNoteService  statementReadJustNoteService;
	@Resource
	private StatementOrderService  statementOrderService;
	
	
	/**
	 * 列表展示自营对账单
	 * @param statementSelfrun
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "list")
	public String list(StatementSelfrun statementSelfrun, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		QueryFilter queryFilter = getQuerFilter(request, statementSelfrun);
		Page<StatementSelfrun> page = (Page<StatementSelfrun>) statementSelfrunService
				.find(queryFilter);
		model.addAttribute("page", page);
		model.addAttribute("statementSelfrun", statementSelfrun);
		return "/biz/statement/statementSelfrun/list";
	}
	/**
	 * ajax确认自营对账单
	 * @param statementSelfrun
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "reconciliation")
	public void reconciliation(StatementSelfrun statementSelfrun, HttpServletRequest request,
			HttpServletResponse response, Model model) throws IOException {
		StringBuilder jsonStr = new StringBuilder();
		response.setCharacterEncoding("utf-8");
		String contextPath=request.getContextPath();
		PrintWriter out = response.getWriter();
		try {
			statementSelfrun.setIsReconciliation(BizStatementConstant.ISRECONCILIATION_);
			
			statementSelfrunService.update(statementSelfrun);
			jsonStr.append("{success:true}");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			jsonStr.append("{success:false}");
		}
		out.print(jsonStr.toString());
		out.flush();
		out.close();
		
	}
	/**
	 * 自营对账详细展示
	 * @param statementSelfrun
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "view")
	public String view(StatementSelfrun statementSelfrun, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		String id = statementSelfrun.getId().toString();
		StatementSelfrun selfrun = statementSelfrunService.get(id);
		if(BizStatementConstant.RECEIVE.equals(selfrun.getBillType())){
			Map map = new HashMap();
			map.put("billNo", selfrun.getBillNo());
			StatementReceiveNote statementReceiveNote = statementReceiveNoteService.getByBillNo(map);
			model.addAttribute("statementReceiveNote",statementReceiveNote);
		}
		if(BizStatementConstant.READJUST.equals(selfrun.getBillType())){
			Map map = new HashMap();
			map.put("billNo", selfrun.getBillNo());
			StatementReadJustNote statementReadJustNote =statementReadJustNoteService.getByBillNo(map);
			model.addAttribute("statementReadJustNote",statementReadJustNote);
		}
		if(BizStatementConstant.ORDER.equals(selfrun.getBillType())){
			Map map = new HashMap();
			map.put("SelfrunNoteNo", selfrun.getBillNo());
			StatementOrder statementOrder =statementOrderService.getByBillNo(map);
			model.addAttribute("statementOrder",statementOrder);
		}
		
		model.addAttribute("statementSelfrun", selfrun);
		return "/biz/statement/statementSelfrun/view";
	}
	
	public StatementSelfrunService getStatementSelfrunService() {
		return statementSelfrunService;
	}

	public void setStatementSelfrunService(
			StatementSelfrunService statementSelfrunService) {
		this.statementSelfrunService = statementSelfrunService;
	}
	
	public StatementReceiveNoteService getStatementReceiveNoteService() {
		return statementReceiveNoteService;
	}
	public void setStatementReceiveNoteService(
			StatementReceiveNoteService statementReceiveNoteService) {
		this.statementReceiveNoteService = statementReceiveNoteService;
	}
	
	public StatementReadJustNoteService getStatementReadJustNoteService() {
		return statementReadJustNoteService;
	}
	public void setStatementReadJustNoteService(
			StatementReadJustNoteService statementReadJustNoteService) {
		this.statementReadJustNoteService = statementReadJustNoteService;
	}
}
